import { createRouter, createWebHistory } from 'vue-router'
import web3Interceptor from '@/utils/web3Interceptor'
import authService from '@/utils/auth'

// 布局组件
import MainLayout from '@/layouts/MainLayout.vue'

// 路由配置
const routes = [
  // 充值页面
  {
    path: '/demo',
    name: 'Demo',
    component: () => import('@/views/web3/Web3Demo.vue'),
    meta: {
      requiresAuth: false,
      title: 'web3'
    }
  },
  // 充值页面
  {
    path: '/deposit',
    name: 'Deposit',
    component: () => import('@/views/cash/deposit.vue'),
    meta: {
      requiresAuth: true,
      requiresWeb3: true,
      title: '充值'
    }
  },
  // 提现页面
  {
    path: '/withdraw',
    name: 'Withdraw',
    component: () => import('@/views/cash/withdraw.vue'),
    meta: {
      requiresAuth: true,
      title: '提现'
    }
  },
  // 我的团队
  {
    path: '/team',
    name: 'Team',
    component: () => import('@/views/mine/team.vue'),
    meta: {
      requiresAuth: true,
      requiresWeb3: true,
      title: '团队'
    }
  },
  // 绩效管理
  {
    path: '/performance',
    name: 'Performance',
    component: () => import('@/views/mine/performance.vue'),
    meta: {
      requiresAuth: true,
      title: '绩效管理'
    }
  },
  // 交易记录页面
  {
    path: '/transactions',
    name: 'Transactions',
    component: () => import('@/views/cash/transactions.vue'),
    meta: {
      requiresAuth: true,
      title: '交易记录'
    }
  },
  // 币种流水记录页面
  {
    path: '/record',
    name: 'Record',
    component: () => import('@/views/cash/record.vue'),
    meta: {
      requiresAuth: true,
      title: '币种流水'
    }
  },
  {
    path: '/pool-join',
    name: 'PoolJoin',
    component: () => import('@/views/home/pool-join.vue'),
    meta: {
      requiresAuth: false,
      title: '参与分红'
    }
  },
  {
    path: '/rank',
    name: 'Rank',
    component: () => import('@/views/home/ranking.vue'),
    meta: {
      requiresAuth: false,
      title: '排名'
    }
  },
  {
    path: '/vip',
    name: 'Vip',
    component: () => import('@/views/home/vip.vue'),
    meta: {
      requiresAuth: false,
      title: 'VIP特权'
    }
  },
  {
    path: '/mining',
    name: 'Mining',
    component: () => import('@/views/home/mining.vue'),
    meta: {
      requiresAuth: false,
      title: '挖矿'
    }
  },
  {
    path: '/finance',
    name: 'Finance',
    component: () => import('@/views/finance/index.vue'),
    meta: {
      requiresAuth: false,
      title: '理财'
    }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/views/mine/setting.vue'),
    meta: {
      requiresAuth: true,
      title: '设置'
    }
  },
  {
    path: '/address',
    name: 'Address',
    component: () => import('@/views/mine/address.vue'),
    meta: {
      requiresAuth: true,
      title: '收货地址'
    }
  },
  {
    path: '/deal-pass',
    name: 'DealPass',
    component: () => import('@/views/mine/deal-pass.vue'),
    meta: {
      requiresAuth: true,
      title: '交易密码'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/mine/about.vue'),
    meta: {
      requiresAuth: true,
      title: '关于我们'
    }
  },
  {
    path: '/agreement/user',
    name: 'UserAgreement',
    component: () => import('@/views/agreement/user.vue'),
    meta: {
      requiresAuth: true,
      title: '用户协议'
    }
  },
  {
    path: '/agreement/privacy',
    name: 'PrivacyPolicy',
    component: () => import('@/views/agreement/privacy.vue'),
    meta: {
      requiresAuth: true,
      title: '隐私政策'
    }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('@/views/mine/order-all.vue'),
    meta: {
      requiresAuth: true,
      title: '全部订单'
    }
  },
  {
    path: '/order/:id',
    name: 'OrderDetail',
    component: () => import('@/views/mine/order-detail.vue'),
    meta: {
      requiresAuth: true,
      title: '订单详情'
    }
  },
  {
    path: '/refund',
    name: 'Refund',
    component: () => import('@/views/refund/index.vue'),
    meta: {
      requiresAuth: true,
      title: '申请售后'
    }
  },
  {
    path: '/project/:id',
    name: 'ProjectDetail',
    component: () => import('@/views/home/project-detail.vue'),
    meta: {
      requiresAuth: false,
      title: '项目详情'
    }
  },
  {
    path: '/products',
    name: 'Products',
    component: () => import('@/views/home/products.vue'),
    meta: {
      requiresAuth: false,
      title: '全部商品'
    }
  },
  {
    path: '/product/:id',
    name: 'ProductDetail',
    component: () => import('@/views/home/product-detail.vue'),
    meta: { 
      requiresAuth: false,
      title: '商品详情'
    }
  },
  {
    path: '/product/:id/reviews',
    name: 'ProductReviews',
    component: () => import('@/views/home/product-reviews.vue'),
    meta: {
      requiresAuth: false,
      title: '商品评价'
    }
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: () => import('@/views/checkout/index.vue'),
    meta: {
      requiresAuth: true,
      title: '结算'
    }
  },
  {
    path: '/customer-service',
    name: 'CustomerService',
    component: () => import('@/views/service/index.vue'),
    meta: {
      title: '客户服务'
    }
  },
  {
    path: '/circulation/points-exchange',
    name: 'PointsExchange',
    component: () => import('@/views/circulation/points-exchange.vue'),
    meta: {
      requiresAuth: true,
      title: '积分兑换'
    }
  },
  {
    path: '/circulation/exchange',
    name: 'CurrencyExchange',
    component: () => import('@/views/circulation/exchange.vue'),
    meta: {
      requiresAuth: true,
      title: '币种兑换'
    }
  },
  {
    path: '/circulation/points-record',
    name: 'PointsRecord',
    component: () => import('@/views/circulation/points-record.vue'),
    meta: {
      requiresAuth: true,
      title: '积分记录'
    }
  },
  {
    path: '/prize-records',
    name: 'PrizeRecords',
    component: () => import('@/views/lucky_draw/prize-records.vue'),
    meta: {
      requiresAuth: true,
      title: '中奖记录'
    }
  },
  {
    path: '/',
    component: MainLayout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页', icon: 'home' }
      },
      {
        path: 'circulation',
        name: 'Circulation',
        component: () => import('@/views/circulation/index.vue'),
        meta: {
          requiresAuth: false,
          title: '流通', icon: 'swap'
        }
      },
      {
        path: 'active',
        name: 'LuckyDraw',
        component: () => import('@/views/mine/performance.vue'),
        meta: {
          requiresAuth: false,
          title: '活动', icon: 'active'
        }
      },
      {
        path: 'mine',
        name: 'Mine',
        component: () => import('@/views/mine/index.vue'),
        meta: {
          requiresAuth: true,
          title: '我的', icon: 'user'
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫，检查钱包连接状态和用户登录状态
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 如果是首页或需要Web3的页面，检查钱包连接状态
  if (to.path === '/home' || to.path === '/' || to.meta.requiresWeb3) {
    // 尝试连接钱包，但不阻止导航
    web3Interceptor.checkWalletConnection();
  }

  // 检查路由是否需要身份验证
  if (authService.routeRequiresAuth(to)) {
    // 处理需要身份验证的路由
    const canProceed = await authService.handleAuthRequired(to);
    if (canProceed) {
      next();
    } else {
      // 不继续导航，因为handleAuthRequired已经处理了重定向
      next(false);
    }
  } else {
    // 不需要身份验证的路由，直接放行
    next();
  }
})

export default router